package com.yzr.common.thread.pool;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Component;

import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

/**
 * @description
 * @author: yx-0173
 * @date: 2021-07-21 18:46
 **/
@Component
public class ThreadPoolExcutorConfig {

    @Value("${thread-pool.corePoolSize:2}")
    private Integer corePoolSize;

    @Value("${thread-pool.queueSize:3}")
    private Integer queueSize;

    @Value("${thread-pool.keepAliveTime:60}")
    private Long keepAliveTime;


    @Bean("worker_pool")
    public ThreadPoolExecutor threadPool() {
        // 初始化线程池
        return new ThreadPoolExecutor(corePoolSize, Runtime.getRuntime().availableProcessors(), keepAliveTime,
                TimeUnit.SECONDS, new LinkedBlockingQueue<>(queueSize), new ThreadPoolExecutor.AbortPolicy());
    }
}
